
package testpluginfile.popup.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;

public class ExpandFileMots {

	private static void log(String log) {
		System.out.println(log);
	}
	
	protected static int getNumLigne(String ligne) {
		int nb = - 1 ;
		if (ligne != null && ligne.startsWith("m")) {
			String arr[] = ligne.split(" ", 2);
			String firstWord = arr[0];
			firstWord = firstWord.replace("m", "");
			int index = firstWord.indexOf('-');
			if (index != -1) {
				firstWord = firstWord.substring(0, index);
			}
			log(firstWord) ;
			try {
				nb = Integer.parseInt(firstWord);
			}
			catch (Exception e) {
				nb = -1 ;
			}
		}
		return nb ;
	}
	

	protected static int getMaxNumLigne (BufferedReader reader) {
	int nb = - 1 ;
	String inputLine = "" ;
	int numeroMot = 0, numeroMotMax = 0 ;
	try {
		while ((inputLine = reader.readLine()) != null) {
			numeroMot = getNumLigne(inputLine);
			numeroMotMax = Math.max(numeroMot, numeroMotMax);
			log(numeroMot + "----------" + inputLine);
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	return numeroMotMax ;
	}

	
	protected static String getNumeroFormate(int nb) {
	String formate = "" ;
	DecimalFormat df = new DecimalFormat( "00000" );
	return df.format(nb) ;
	}
	
	//
	public static void main (String args[]) {
		System.out.println("toto");
		String nomFichier = "d://test.txt" ;
		BufferedReader in;
		StringBuilder  newfilecontent = new StringBuilder()   ;
	    int nbLignes = 0 ;
	    int numeroMot ;
	    int numMaxMot = 0;
	    String numMaxMotformatte = ""  ;
	    
	    try {
	    	numMaxMot = getMaxNumLigne(new BufferedReader(new FileReader(nomFichier)));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			log("Erreure fatale !");
			System.exit(0);
		}
	    
		try {
			log("lecture de " + nomFichier);
			in = new BufferedReader(new FileReader(nomFichier));
			String inputLine = "" ;
						
			while ((inputLine = in.readLine()) != null) {
				inputLine = inputLine.trim();
				numeroMot = getNumLigne(inputLine);
				if (numeroMot == -1 && ! inputLine.equals("")) {
					numMaxMot++;
					numMaxMotformatte = getNumeroFormate(numMaxMot);
					newfilecontent.append("\nm" + numMaxMotformatte + " " + inputLine) ;
					for (int i = 0 ; i < 5 ; i++) {
						newfilecontent.append("\nm" + numMaxMotformatte + "- " ) ;
					}					
				} else {
					newfilecontent.append("\n" + inputLine) ;
				}
				log(numMaxMotformatte + "----------" + inputLine);
			}
			in.close();
			
			//écriture
			try {
        		FileWriter fstream = new FileWriter(nomFichier);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(newfilecontent.toString());
                out.close() ;
            } catch (IOException ex) {
            	System.err.println(ex.toString());
            }			
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
}

